package ${domain}.module.wsys.mgr;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.github.pagehelper.util.StringUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import ${domain}.frame.base.ErrorType;
import ${domain}.frame.auth.Token;
import ${domain}.frame.utils.IDgenerator;
import ${domain}.frame.utils.MapperUtil;
import ${domain}.frame.utils.Message;
import ${domain}.frame.utils.ValidationUtil;
import ${domain}.module.wsys.ent.File;
import ${domain}.module.wsys.mpr.FileMapper;
import ${domain}.module.wsys.req.FileCreateRequest;
import ${domain}.module.wsys.req.FileDeleteRequest;
import ${domain}.module.wsys.req.FileFindRequest;
import ${domain}.module.wsys.req.FileGetRequest;
import ${domain}.module.wsys.rsp.FileCreateResponse;
import ${domain}.module.wsys.rsp.FileDeleteResponse;
import ${domain}.module.wsys.rsp.FileFindResponse;
import ${domain}.module.wsys.rsp.FileGetResponse;

/**
 * FILE - 文件
 *
 * @author wangbing
 * @version 0.0.1
 * @since 2017-01-01
 */
@Transactional
@Service
public class FileManagerImpl implements FileManager {

    @Autowired
    private FileMapper fileMapper;

    /**
     * 插入
     *
     * @param request 请求对象
     * @param token   令牌
     * @return 响应
     */
    public FileCreateResponse create(FileCreateRequest request, Token token) {
        FileCreateResponse response = new FileCreateResponse();

        ValidationUtil.validate(request, response);
        if (response.hasError()) {
            return response;
        }

        long id = IDgenerator.nextId();
        File entity = MapperUtil.map(request, File.class);
        entity.setId(id);

        long result = fileMapper.insert(entity, token);
        if (1L != result) {
            response.addError(ErrorType.BUSINESS_ERROR, Message.CREATE_FAILURE);
            return response;
        }
        response.setId(id);

        return response;
    }

    /**
     * 逻辑删除
     *
     * @param request 请求对象
     * @param token   令牌
     * @return 响应
     */
    public FileDeleteResponse delete(FileDeleteRequest request, Token token) {
        FileDeleteResponse response = new FileDeleteResponse();

        ValidationUtil.validate(request, response);
        if (response.hasError()) {
            return response;
        }

        long result = fileMapper.delete(request.getId(), token);
        if (1L != result) {
            response.addError(ErrorType.BUSINESS_ERROR, Message.DELETE_FAILURE);
            return response;
        }
        response.setResult(result);

        return response;
    }

    /**
     * 查询
     *
     * @param request 请求对象
     * @param token   令牌
     * @return 响应
     */
    @Transactional(readOnly = true)
    public FileFindResponse find(FileFindRequest request, Token token) {
        FileFindResponse response = new FileFindResponse();

        ValidationUtil.validate(request, response);
        if (response.hasError()) {
            return response;
        }

        if (request.getPageSize() != 0) {
            PageHelper.startPage(request.getPageNumber(), request.getPageSize());
        }
        if (StringUtil.isNotEmpty(request.getSortKey())) {
            PageHelper.orderBy(request.getSortKey() + " " + request.getSortType());
        }
        PageInfo<File> pageInfo = new PageInfo<>(fileMapper.find(request, token));

        response.setResult(pageInfo.getList());
        response.setTotalCount(pageInfo.getTotal());

        return response;
    }

    /**
     * 获得对象
     *
     * @param request 请求对象
     * @param token   令牌
     * @return 响应
     */
    @Transactional(readOnly = true)
    public FileGetResponse get(FileGetRequest request, Token token) {
        FileGetResponse response = new FileGetResponse();

        ValidationUtil.validate(request, response);
        if (response.hasError()) {
            return response;
        }

        File po = fileMapper.getById(request.getId(), token);

        if (po != null) {
            response.setFile(po);
        } else {
            response.addError(ErrorType.BUSINESS_ERROR, Message.GET_FAILURE);
        }

        return response;
    }
}
